package com.briup.pts.service.impl;

import com.briup.pts.config.exception.ServiceException;
import com.briup.pts.domain.bean.QuarantineRegistration;
import com.briup.pts.mapper.QuarantineMapper;
import com.briup.pts.service.QuarantineService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author LENOVO
 * @description data 2024/11/26--15:09
 */
@Service
@RequiredArgsConstructor
public class QuarantineServiceImpl implements QuarantineService {
    private final QuarantineMapper mapper;
    @Override
    public PageInfo<QuarantineRegistration> pageQueryByCondition(Integer pageNum, Integer pageSize,String bQualified,
                                                                 String grMechanism) {
        // 使用过PageHelper开启分页
        try (Page<QuarantineRegistration> ignore = PageHelper.startPage(pageNum, pageSize)){
            // 根据条件查询数据库中的数据
            List<QuarantineRegistration> quarantine = mapper.queryPage(bQualified,grMechanism);
            // 将查询的结果数据放入到PageInfo中返回
            return new PageInfo<>(quarantine);
        }
    }

    @Override
    public void saveOrUpdate(QuarantineRegistration registration) {
        if (registration.getGrId()==null){
            mapper.addRegistration(registration);
        }else {
            mapper.UpdateRegistration(registration);
        }
    }

    @Override
    public void deleteById(Integer grId) {
        mapper.deleteById(grId);
    }

    @Override
    public void deleteByIdAll(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new ServiceException("没有需要删除的书籍");
        }
        mapper.deleteByIdAll(ids);
    }
}
